package com.xsir.ditan.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xsir.ditan.admin.entity.Menu;
import com.xsir.ditan.admin.mapper.MenuMapper;
import com.xsir.ditan.admin.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xsir.ditan.admin.vo.MenuListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xsir
 * @since 2021-02-02
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    
    @Override
    public List<MenuListVo> getNavMenu() {
        return getMenuList(0);
    }
    
    @Override
    public List<MenuListVo> getMenuList(int pid) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", pid);
        List<Menu> menus = list(wrapper);
        List<MenuListVo> menuListVoList = new ArrayList<>();
        if(!menus.isEmpty()) {
            menus.forEach(menu -> {
                MenuListVo menuListVo = new MenuListVo();
                BeanUtils.copyProperties(menu, menuListVo);
                List<MenuListVo> children = getMenuList(menu.getId());
                menuListVo.setChildren(children);
                menuListVoList.add(menuListVo);
            });
        }
        return menuListVoList;
    }
}
